import { defineStore } from 'pinia';
import axios from '../api/request.ts';

interface Notification {
    id: number;
    content: string;
}

interface NotificationState {
    notifications: Notification[];
}

export const useNotificationStore = defineStore('notification', {
    state: (): NotificationState => ({
        notifications: [],
    }),
    actions: {
        async fetchNotifications() {
            try {
                const response = await axios.get<Notification[]>('/notifications');
                this.notifications = response.data;
            } catch (error) {
                console.error('Error fetching notifications:', error);
            }
        },
        async createNotification(content: string) {
            try {
                await axios.post('/notifications', { content });
                await this.fetchNotifications();
            } catch (error) {
                console.error('Error creating notification:', error);
            }
        },
        async updateNotification(id: number, content: string) {
            try {
                await axios.put(`/notifications/${id}`, { content });
                await this.fetchNotifications();
            } catch (error) {
                console.error('Error updating notification:', error);
            }
        },
        async deleteNotification(id: number) {
            try {
                await axios.delete(`/notifications/${id}`);
                await this.fetchNotifications();
            } catch (error) {
                console.error('Error deleting notification:', error);
            }
        },
    },
});
